package com.send.data;

import com.alibaba.fastjson.JSONObject;
import com.send.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.jms.Queue;
import java.util.UUID;

/**
 * @classDesc: 功能描述:(生产者代码)
 */
@Component//将Producer注入到容器
@EnableScheduling//定时任务的注解
public class Producer {
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;
    @Autowired//注入
    private Queue queue;
    private int age = 18;
    @Scheduled(fixedDelay = 5000)//每隔5秒钟执行这个方法
    public void send() {
        age++;
        UserEntity userEntity = new UserEntity(System.currentTimeMillis(), UUID.randomUUID().toString(), age);
        String json = new JSONObject().toJSONString(userEntity);//将实体类转换成json字符串
        System.out.println("json:" + json);
        jmsMessagingTemplate.convertAndSend(queue, json);//向指定队列中发送消息
    }
}